package com.wkhyapp.lm.fragment;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lidong.photopicker.intent.PhotoPreviewIntent2;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.GoodsImagesListAdapter;
import com.wkhyapp.lm.adapter.GoodsListAdapter;
import com.wkhyapp.lm.base.SuperFragment;
import com.wkhyapp.lm.contract.ShopPresenter;
import com.wkhyapp.lm.contract.ShopView;
import com.wkhyapp.lm.http.vo.Category;
import com.wkhyapp.lm.http.vo.Follows;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.http.vo.Member;
import com.wkhyapp.lm.utils.EmptyView;
import com.wkhyapp.lm.utils.ImgDonwloads;
import com.wkhyapp.lm.view.GoodsInfoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ShopGoodsImageFragment extends SuperFragment<ShopPresenter> implements ShopView {
    @BindView(R.id.refresh_view)
    SwipeRefreshLayout refresh_view;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    private Integer userId;
    private GoodsImagesListAdapter goodsImagesListAdapter;
    private List<Goods> goodses;
    private int mCurrentPage = 1;
    View empty_view;
    private Integer typeId = 0;
    @Override
    protected ShopPresenter createPresenter() {
        return mPresenter = new ShopPresenter(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        goodsImagesListAdapter.setCallBack(new GoodsImagesListAdapter.callBack() {
            @Override
            public void info(Goods item) {
                goTo(GoodsInfoActivity.class,item.getId());

            }


        });
        goodsImagesListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mCurrentPage = mCurrentPage+1;
                mPresenter.getDataMore(mCurrentPage,userId, typeId);

            }
        });
        refresh_view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mPresenter.getData(userId,typeId);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_shop_goods_image;
    }

    @Override
    public void initUI() {
        goodses = new ArrayList<>();
        empty_view = EmptyView.getView(getContext(),R.layout.empty_view,R.drawable.wuzhuangtai,"暂无上架商品");
        goodsImagesListAdapter = new GoodsImagesListAdapter(R.layout.item_shop_goods_image,goodses);
        refresh_view.setColorSchemeColors(Color.parseColor("#FFEC48"), Color.parseColor("#FFEC48"));
        GridLayoutManager manager = new GridLayoutManager(getContext(),4);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return 1;
                }

                if (position == recycler_view.getAdapter().getItemCount() - 1) {
                    return 4;
                }
                return 1;
            }
        });
        recycler_view.setLayoutManager(manager);
        recycler_view.setAdapter(goodsImagesListAdapter);
        refresh_view.setRefreshing(false);
        mPresenter.getShop(userId);
        mPresenter.getData(userId,0);
        showLoad("正在加载数据...");
        mPresenter.checkVersion();
    }

    public void refleshGoodsByTyp(Integer typeId) {
        goodses.clear();
        showLoad("正在加载数据...");
        this.typeId = typeId;
        mPresenter.getData(userId, typeId);
    }

    @Override
    public void setGoods(List<Goods> list) {
        goodses.clear();
        if(list!=null&&list.size()>0){
            goodses.addAll(list);
            goodsImagesListAdapter.removeFooterView(empty_view);
        }else{
            goodsImagesListAdapter.removeFooterView(empty_view);
            goodsImagesListAdapter.addFooterView(empty_view);
        }
        goodsImagesListAdapter.notifyDataSetChanged();
        refresh_view.setRefreshing(false);
        goodsImagesListAdapter.loadMoreComplete();
    }

    @Override
    public void setGoodsMore(List<Goods> list) {
        if(list.size()==0){
            goodsImagesListAdapter.loadMoreComplete();
            goodsImagesListAdapter.loadMoreEnd();
        }else{
            goodses.addAll(list);
            goodsImagesListAdapter.notifyDataSetChanged();
            goodsImagesListAdapter.loadMoreComplete();
        }
    }

    @Override
    public void setCategoory(List<Category> list) {

    }

    @Override
    public void setShop(Member member) {

    }

    @Override
    public void setFollows(Follows str) {

    }

    @Override
    public void succ() {

    }

    @Override
    public void checkVersion() {

    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }
}
